﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Queue object -- provide an interface to the customer queue database table
/// </summary>
public class Queue
{
    SqlConnection conn = Utils.getDBConnection();

    private int que_id = 0;
    private short que_order = 0;
    private int cus_id = 0; 
    private int movie_id = 0;

    public Queue()
    {

    }

    /// <summary>
    /// List of all getters and setter for this class
    /// </summary>
    public int Que_ID
    {
        get { return que_id; }
        set { que_id = value; }
    }
    public short Que_Order
    {
        get { return que_order; }
        set { que_order = value; }
    }
    public int Cus_ID
    {
        get { return cus_id; }
        set { cus_id = value; }
    }
    public int Movie_ID
    {
        get { return movie_id; }
        set { movie_id = value; }
    }


   //methods
    
    /// <summary>
    /// This is the data access method for the customer queue.
    /// </summary>
    /// <param name="cs">Customer object</param>
    /// <returns>A list of Queue objects</returns>
    public List<Queue> getQueueList(Customer cs)
    {
        List<Queue> queueList = new List<Queue>();

        SqlCommand cmd = new SqlCommand("dbo.mov_getCustomerQueue", conn);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@email", cs.Email);
        try
        {
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

			//Retrieve the queue table from the database, and populate an object containing Queue objects for each row
            while (reader.Read())
            {
				//Create a new Queue object
                Queue q = new Queue();
				//Populate the object with the database row
                q.Que_ID = Convert.ToInt32(reader["que_id"]);
                q.Que_Order = Convert.ToInt16(reader["que_order"]);
                q.Cus_ID = Convert.ToInt32(reader["cus_id"]);
                q.Movie_ID = Convert.ToInt32(reader["movie_id"]);
				//Add the new Queue object to the list collection
                queueList.Add(q);
            }

			//Clean up
            reader.Close();
            conn.Close();

        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception thrown: " + ex.Message);
        }

        return queueList;
    }

    /// <summary>
    /// Get a dataset containing the movie information for movies in this customer's queue.
    /// </summary>
    /// <param name="email">The email address identifing the customer</param>
    /// <returns>DataSet containing movie information</returns>
    public DataSet getCustomerQueueForDisplay(string email)
    {
        DataSet ds = null;

        try
        {
            //prepare SQL call
            SqlCommand storedProcCommand = new SqlCommand("dbo.mov_getCustomerQueueForDisplay", conn);
            storedProcCommand.CommandType = System.Data.CommandType.StoredProcedure;
            storedProcCommand.Parameters.AddWithValue("@email", email);
            conn.Open();

            //exec SQL
            SqlDataReader reader = storedProcCommand.ExecuteReader();

			//Convert the SQL results to the standard dataset output format
			//This will allow another function to bind to this dataset
            ds = Utils.DataReaderToDataSet(reader);


        }
        catch (Exception ex)
        {

			//If an error occured, make sure to clean up
            ds = null;
            conn.Close();
            Console.WriteLine("Error retrieving customer data:" + ex.Message);

        }

        conn.Close();
        return ds;
    }
}

